<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="add" v-if="isShow(128)">新 增</el-button>
        <!-- <el-button type="primary" class="myBgGreen">导出Excel</el-button> -->
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">
      <el-col :span="7">
        <el-input  v-model="kw" placeholder="请输入关键字"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="list"
        :border = true
        :stripe = true
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          label="序号"
          :index="indexMethod"
          width="100"
          type="index"
          >
        </el-table-column>
        <el-table-column
          prop="user_name"
          label="姓名"
          width="150"
          >
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          width="80"
          >
          <template scope="scope">
            <span v-if="scope.row.sex==0">女</span>
            <span v-if="scope.row.sex==1">男</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          width="150"
          label="手机"
          >
        </el-table-column>
        <el-table-column
          prop="dept_name"
          label="所属部门"
          >
        </el-table-column>
        <el-table-column
          prop="role_name"
          label="职务"
          >
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <template scope="scope">
            <el-button @click="getDetail(scope.row.staff_id)" class="look" type="text" size="small">查看</el-button> |
            <el-button type="text primary" @click="modifyData(scope.row.staff_id)" class="look" size="small" v-if="isShow(129)">编辑</el-button> |
            <el-button type="text warning" @click="deleteData(scope.row.staff_id)" class="look" size="small" >删除</el-button> 
          </template>
        </el-table-column>
      </el-table>
      <div class="block">                    
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page_num"
          :page-sizes="[10, 15]"
          :page-size="page_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div> 
    </el-row>

    <!-- 确定删除弹框开始 -->
    <el-dialog title="删除" class="myRoleDialog" :visible.sync="deleteDialog">
      <div>是否确定删除这条数据？</div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="deleteDialog = false">取 消</el-button>
        <el-button type="primary" class="myBgGreen" @click="sureDelete">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 确定删除弹框结束 -->
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
</style>

<script>

  import { getStaffList , deleteStaff } from '@/api/staff';
  import { extractAreaIds } from '@/utils/misc';
  import { getRegion } from '@/utils/localstorage';
  export default {
    data(){
      return{
        title: "员工管理",
        loading:false,
        kw:"",
        list:[],

        page_num: null, //当前页码
        page_size: null, //每页显示多少条
        total:null, //总条数  

        lookObj:{},
        selectedBm:-1,
        id:null,
        index:null,
        idArr:[],
        multipleSelection: [],
        options: [],
        deleteDialog:false,
        delete_id:''
      }
    },
    created(){
      document.title=this.title;
      this.fetchStaffList();
          
    },
    methods:{
      fetchStaffList(){
        this.loading = true;
        let params = {
          page_num: this.page_num,
          page_size: this.page_size,
          kw: this.kw
        };
        let vm = this;
        getStaffList(params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;

          vm.loading = false;
        }).catch(error => {
          vm.loading = false;
        })
      },
      handleSizeChange(val) {
        this.page_size = val;
        this.fetchStaffList();
      },
      handleCurrentChange(val) {
        let pageNum=window.localStorage.getItem("pageNum");
        if(pageNum){
          this.page_num = Number(pageNum);
          window.localStorage.removeItem("pageNum");
        }else{
          this.page_num = val;
        }
        this.fetchStaffList();
      },
      fuzzySearch() {
        this.fetchStaffList();
      },
      changeSelectBm:function(select){
        this.selectedBm = select;
      },
      add:function(){
        this.$router.push({
          path:'/ygglComonents',
          query: {type: 'add', pageNum: this.page_num}
        })
      },
      getDetail : function(id){
        this.$router.push({
          path:'/ygglComonents',
          query: {type: 'info', id: id, pageNum: this.page_num}
        })
      },
      modifyData:function(id){                 
        this.$router.push({
          path:'/ygglComonents',
          query: {type: 'update', id: id, pageNum: this.page_num}
        })
      },
      deleteData(u_id){
        this.delete_id = u_id;
        this.deleteDialog=true;
      },
      sureDelete:function(){
        let vm=this;
        deleteStaff(vm.delete_id).then(response => {
          let res=response.data;
          vm.$message.success("执行成功！");
          vm.fetchStaffList();
        }).catch(error => {
          vm.$message.error("执行失败！");
        })
        this.deleteDialog=false;        
      },
   
      // allDelete:function(event){
      //   if(this.multipleSelection!=0){
      //     var pageArr = {
      //       page_num : this.currentPage-1
      //     }
          
      //     var arrId = {
      //       staff_ids : this.idArr.join(",")
      //     }
      //     //console.log(JSON.stringify(arrId)) 
      //     optVue.deleteYgData(this,this.index,Tips,pageArr,arrId);
          
      //     this.idArr = [];
   
      //   }
      //   else{
      //     Tips.init({
      //       type:"fail",
      //       message:"请选择要删的项",
      //       class:"on"
      //     });
      //   }
      // },
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].area_id)
          }
        }else{
          this.idArr = [];
        }
       
        //console.log(JSON.stringify(this.idArr))
      },
      tableRowClassName(row, index) {
          //把每一行的索引放进row
          row.index = index
      }, 
      indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      }

    }

  }

</script>



